<?php

namespace app\common\service\store;

use app\admin\model\order\Order as OrderModel;
use app\admin\model\store\Pink;
use app\admin\model\store\PinkConfig as PinkConfigModel;
use app\common\service\BaseService;
use app\common\service\OrderService;
use app\validate\PinkCheck;
use think\Db;

class PinkService extends BaseService
{
    //分页配置参数
    protected static $pagesize;

    public function __construct()
    {
        self::$pagesize = config('setting.pagesize');
    }

    //前台拼团列表
    public static function pinkListInfo()
    {
        $where [] = ['pink_status', '=', '0'];//活动开启的
        //活动信息
        $field = [
            'id',//活动id
            'goods_sku_id',//skuid
            'goods_img',//商品图片
            'pink_name',//活动标题
            'pink_name',//拼团名称
            'goods_carousel',//商品详情图
            'pink_price',//拼团价格
            'pink_people',//拼团人数几人团
            'goods_sku_price',//商品原价
            'total_num',//拼团销售总数
            'pink_sku_stock',//库存
            'quota',//限额
        ];
        $data = PinkConfigModel::where($where)
            ->order('sort', 'desc')
            ->field($field)
            ->paginate(self::$pagesize);
        $data && $data = $data->toArray();
        if (empty($data['data'])) {
            return self::fail('查询失败');
        }
        return self::success('查询成功', $data);
    }

    /**
     * 查询拼团活动详情
     * Author:William
     * @param $pinkId 拼团活动id
     * @return int
     */
    public static function pinkDesc($pinkId)
    {
        $data = PinkConfigModel::pinkInfo($pinkId);
        if (!empty($data['goods_carousel'])){
            $data['goods_carousel']=explode('|',$data['goods_carousel']);
        }
        //校验参数
        return self::checkPamasNull($data);
    }

    /** 创建拼团,拼团校验 判断拼团是否已经创建，已经创建拼团
     * Author:William
     * @param array $params
     * @return array|bool
     */
    public static function pinkCreate(array $params = [])
    {
        //创建团编号
        $params['pink_no'] = self::getPinkNo();
        //创建拼团订单成为团长
        $params ['is_pink_master'] = 1;
        //订单区别码
        $params['order_type_code'] = 2;
        //查询拼团价
        $pinkPirce = PinkConfigModel::where('id', $params['pink_id'])
            ->field(['pink_price'])
            ->value('pink_price');
        $params['pay_price'] = $pinkPirce;

        //创建拼团订单  拼团statusCode == 2
        $data = OrderService::orderCreate($params);
        if ($data['code'] == 400) {
            return self::fail($data['message']);
        }
        Pink::create($params);
        return $data;
    }

    /**  参与拼团
     * Author:William
     * @param array $params
     * @return array|bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function joinPink($params = [])
    {
        try {
            validate(PinkCheck::class)->check($params);
        } catch (ValidateException $e) {
            // 验证失败 输出错误信息
            $msg = implode($e->getError(), ',');
            return self::fail($msg);
        }

        //查询参团人数限制
        //查询拼团价  拼团人数
        $data = PinkConfigModel::where('id', $params['pink_id'])->field(['pink_price', 'pink_people'])->find();
        $data && $data = $data->toArray();
        //统计成团人数
        $pinkOkCount = OrderModel::where('pink_no', $params['pink_no'])->count('pink_no');
        //人数足够去支付
        if ($pinkOkCount == $data['pink_people']) {
            return self::success('参团人数已满');
        } else {
            //价格赋值
            $params['pay_price'] = $data['pink_price'];
            $params['order_type_code'] = 2;
            return OrderService::orderCreate($params);
        }
    }


    /**生成團編號
     * Author:William
     * @return string
     */
    public static function getPinkNo()
    {
        $pink_no = 'pinK' . time() . mt_rand(1000, 9999);
        return $pink_no;
    }





}